// import dayjs from "dayjs";

/**
 * 时间日期转换
 * @param date 当前时间，new Date() 格式
 * @param format 需要转换的时间格式字符串
 * @description format 字符串随意，如 `YYYY-mm、YYYY-mm-dd`
 * @description format 季度："YYYY-mm-dd HH:MM:SS QQQQ"
 * @description format 星期："YYYY-mm-dd HH:MM:SS WWW"
 * @description format 几周："YYYY-mm-dd HH:MM:SS ZZZ"
 * @description format 季度 + 星期 + 几周："YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
 * @returns 返回拼接后的时间字符串
 */
// export function formatDate(date, format) {
//   // 日期不存在，则返回空
//   if (!date) {
//     return "";
//   }
//   // 日期存在，则进行格式化
//   if (format === undefined) {
//     format = "YYYY-MM-DD HH:mm:ss";
//   }
//   return dayjs(date).format(format);
// }
// 获取当前往前N天的日期
export function getLast30day(day) {
  var today = new Date();
  var targetday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds);

  var tYear = today.getFullYear();
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  if (tMonth < 10) tMonth = '0' + tMonth;
  if (tDate < 10) tDate = '0' + tDate;
  return tYear + '-' + tMonth + '-' + tDate;
}
export function getDays() {
  var now = new Date();
  var yesterday = new Date(now);
  yesterday.setDate(yesterday.getDate() - 1);
  return yesterday.toISOString().split('T')[0];
}
// 获取前N个月的年月日
export function getXmonthToday(type) {
  // type 0 是当天 -1 是上个月   1是下个月
  var now = new Date(); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
  var year = now.getFullYear(); //getYear()+1900=getFullYear()
  var month = now.getMonth() + 1; //0-11表示1-12月
  var day = now.getDate(); // 当天日期
  if (parseInt(month) < 10) {
    month = '0' + month;
  }
  if (parseInt(day) < 10) {
    day = '0' + day;
  }
  now = year + '-' + month + '-' + day; // 如果取当月日期可直接 return 返回
  let msum = Math.abs(type);
  var preMonth = 0; //月值
  let preSizes = 0; //上月总天数
  if (parseInt(msum / 12) === 1) {
    //一年
    preMonth = parseInt(month);

    if (parseInt(month) === 12) {
      preSizes = new Date(year - 1, parseInt(month), 0).getDate();
      if (day > preSizes) {
        return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
      } else {
        return parseInt(year) - 1 + '-' + preMonth + '-' + day;
      }
    } else {
      preSizes = new Date(year - 1, parseInt(month), 0).getDate();
      preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
      if (parseInt(day) > preSizes) {
        return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
      } else {
        return parseInt(year) - 1 + '-' + preMonth + '-' + day;
      }
    }
  } else if (parseInt(msum / 12) > 1) {
    //两年，三年
    preMonth = parseInt(month);
    if (parseInt(month) === 12) {
      preSizes = new Date(year - 1, parseInt(month), 0).getDate();
      if (day > preSizes) {
        return (
          parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + preSizes
        );
      } else {
        return (
          parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day
        );
      }
    } else {
      preSizes = new Date(
        year - parseInt(msum / 12),
        parseInt(month),
        0
      ).getDate();
      if (Math.abs(msum) % 12 === 0) {
        preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
        if (parseInt(day) > preSizes) {
          return (
            parseInt(year) -
            parseInt(msum / 12) +
            '-' +
            preMonth +
            '-' +
            preSizes
          );
        } else {
          return (
            parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day
          );
        }
      } else {
        preMonth = parseInt(preMonth - (Math.abs(msum) % 12));
        preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
        if (parseInt(day) > preSizes) {
          return (
            parseInt(year) -
            parseInt(msum / 12) +
            '-' +
            preMonth +
            '-' +
            preSizes
          );
        } else {
          return (
            parseInt(year) - parseInt(msum / 12) + '-' + preMonth + '-' + day
          );
        }
      }
    }
  } else {
    //小于一年
    if (parseInt(month) === msum) {
      preMonth = parseInt(month) + 1;
      preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
      preSizes = new Date(year - 1, parseInt(month), 0).getDate();
      if (day > preSizes) {
        return parseInt(year) - 1 + '-' + preMonth + '-' + preSizes;
      } else {
        console.warn(parseInt(year) - 1 + '-' + preMonth + '-' + day);
        return parseInt(year) - 1 + '-' + preMonth + '-' + day;
      }
    } else {
      preSizes = new Date(year, parseInt(month), 0).getDate();

      if (parseInt(month) > msum) {
        preMonth = parseInt(month) - msum;
      } else if (parseInt(month) === msum) {
        year = Number(year) - 1;
        preMonth = 12;
      } else {
        year = Number(year) - 1;
        preMonth = 12 - (msum - parseInt(month));
      }
      preMonth = preMonth < 10 ? '0' + preMonth : preMonth; // 获取上个月的值
      if (day > preSizes) {
        return parseInt(year) + '-' + preMonth + '-' + preSizes;
      } else {
        return parseInt(year) + '-' + preMonth + '-' + day;
      }
    }
  }
}
// 获取上个月，并返回年月日
export function getLastToday() {
  var now = new Date(); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
  var year = now.getFullYear(); //getYear()+1900=getFullYear()
  var month = now.getMonth(); //0-11表示1-12月
  var day = now.getDate(); // 当天日期
  let lastDate = '';
  if (parseInt(day) < 10) {
    day = '0' + day;
  }
  if (parseInt(month) == 0) {
    lastDate = parseInt(year) - 1 + '-12-01';
  } else {
    if (parseInt(month) < 10) {
      month = '0' + month;
    }
    lastDate = parseInt(year) + '-' + month + '-01';
  }
  return lastDate;
}
export function formatDateFun(time, type) {
  let beginTime = new Date(time);
  const year = beginTime.getFullYear();
  const month = String(beginTime.getMonth() + 1).padStart(2, '0');
  const day = String(beginTime.getDate()).padStart(2, '0');
  if (type == 'm') return `${year}-${month}`;
  else return `${year}-${month}-${day}`;
}
//根据年月获取本月天数
export function getDaysInMonth(year, month) {
  var date = new Date();
  date.setFullYear(year);
  date.setMonth(month);
  date.setDate(0);
  return date.getDate();
}
//日期：最多不超过3个月
export function isMonthComp(e) {
  let bYear = e[0].split('-');
  let eYear = e[1].split('-');
  let differ = 0;
  if (Number(eYear[0]) == Number(bYear[0]))
    differ = Number(eYear[1]) - Number(bYear[1]);
  else differ = 12 - Number(bYear[1]) + Number(eYear[1]);

  if (differ > 3) {
    return false;
  }
  if (differ == 3) {
    let spaceday = Number(eYear[2]) - Number(bYear[2]);
    if (spaceday >= 1) {
      return false;
    }
  }
  return true;
}
//月：最多不超过3个月
export function isMonthSpace(bYear, eYear, differ) {
  if (bYear[0] < eYear[0]) {
    //跨年
    let spacemonth = 12 - Number(bYear[1]) + 1 + Number(eYear[1]);
    if (spacemonth > 3) {
      return false;
    }
  } else {
    //同年
    let spacemonth = Number(eYear[1]) - Number(bYear[1]);
    if (spacemonth >= 3) {
      return false;
    }
  }
  return true;
}
// 获取最近N个月的年月日
export function getdefaultMoth(n) {
  // 获取当前日期
  const currentDate = new Date();
  const recentMonths = [];
  for (let i = 0; i < n; i++) {
    const date = new Date();
    date.setMonth(currentDate.getMonth() - i); // 计算前 i 个月的日期

    // 格式化日期为 YYYY-MM-DD
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需要 +1
    const day = String(date.getDate()).padStart(2, '0');

    recentMonths.push(`${year}-${month}-${day}`);
  }
  return recentMonths;
}
// 获取最近一周的年月日
export function getdefaultWeek(date) {
  const weekDates = [];
  const dayOfWeek = date.getDay();
  const startOfWeek = new Date(date);

  startOfWeek.setDate(date.getDate() - dayOfWeek);

  for (let i = 0; i < 7; i++) {
    const currentDay = new Date(startOfWeek);
    currentDay.setDate(startOfWeek.getDate() + i);
    weekDates.push(currentDay.toISOString().split('T')[0]); // 格式化为 YYYY-MM-DD
  }
  return weekDates;
}
// 获取今天所属的年以及季度
export function getdefaultQuater() {
  const date = new Date();
  const month = date.getMonth();
  const year = date.getFullYear();
  let quarterStartMonth;
  if (month < 3) {
    quarterStartMonth = 1; // 第一季度
  } else if (month < 6) {
    quarterStartMonth = 2; // 第二季度
  } else if (month < 9) {
    quarterStartMonth = 3; // 第三季度
  } else {
    quarterStartMonth = 4; // 第四季度
  }
  return [year, quarterStartMonth];
}
// 获取今天的日期
export function getTodayDate() {
  const today = new Date();
  const year = today.getFullYear();
  const month = String(today.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需要 +1
  const day = String(today.getDate()).padStart(2, '0');

  const formattedDate = `${year}-${month}-${day}`;
  return formattedDate;
}
